package com.yunhe.serviceImpl;

import com.yunhe.javabean.Permission;
import com.yunhe.javabean.Users;
import com.yunhe.mapper.UserMapper;
import com.yunhe.service.PermissionService;
import com.yunhe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    BCryptPasswordEncoder passwordEncoder;//密码加密类

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询数据返回实体类对应数据
        Users users = userMapper.selectByUserName(username);
        if (users != null) {
            //创建springSecurity主体中存储的账号对象并返回(账号,密码，权限列表)
            UserDetails userDetails = new User(users.getUsername(), users.getPassword(), getAuthority(users.getId()));
            return userDetails;
        }
        return null;
    }

    @Autowired
    PermissionService permissionService;
    //获取权限列表
    public List<GrantedAuthority> getAuthority(int uid) {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        //查询指定用户权限列表
        List<Permission> permissionList = permissionService.findByUid(uid);
        for (Permission p:permissionList ) {
            GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(p.getPermissionName());
            grantedAuthorities.add(grantedAuthority);
        }
        return grantedAuthorities;
    }

    @Override
    public List<Users> findAllUser() {
        return userMapper.selectAllUser();
    }

    @Override
    public Users findById(Integer id) {
        return userMapper.selectById(id);
    }


    @Override
    @Transactional
    public boolean edit(Users users, Integer rid) {

        //将密码加密后修改
        users.setPassword(passwordEncoder.encode(users.getPassword()));
        //修改用户信息
        int i = userMapper.updateById(users);
        //修改角色关联信息
        int i1 = userMapper.updateUserRole(users.getId(), rid);
        return true;
    }

    @Override
    public int deleteById(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    @Transactional
    public boolean add(Users users, Integer rid) {
        //添加数据前将密码加密
        users.setPassword(passwordEncoder.encode(users.getPassword()));

        //向用户表中添加数据
        userMapper.insert(users);
        //向用户角色关联表中添加数据
        userMapper.InsertUserRole(users.getId(), rid);
        return true;
    }
}
